home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 2
/
Mac Magazin and MacEasy Magazine CD - Issue 02.iso
/
Sharewarebibliothek
/
Applikationen
/
Alpha.5.81 folder
/
Tcl
/
SystemCode
/
procs.tcl
< prev
next >
Wrap
Text File
|
1994-06-13
|
11KB
|
456 lines
#==============================================================================
proc normalLeftBracket {} {
insertText "\{"
}
proc normalRightBracket {} {
insertText "\}"
}
bind '\[' <zs> normalLeftBracket
bind '\]' <zs> normalRightBracket
# Select the next or current word. If word already selected, will go to next.
proc hiliteWord {} {
if {[getPos]!=[selEnd]} forwardChar
forwardWord
set start [getPos]
backwardWord
select $start [getPos]
}
bind 'h' <z> hiliteWord
#================================================================================
# Mode variables
#================================================================================
# For mark stack.
set markName 0
set markStack ""
# mapping of windows to current modes.
set winModes("") ""
# making vars local to windows
# 'incomingVars' used to hold old var values that have been overwritten in current window
#================================================================================
# Handle 'flag' and 'var' menu selections.
#================================================================================
proc editFlag {menu item} {
global $item incomingVars
set val [expr ([set $item]-1)*-1]
markMenuItem $menu $item [expr ([set $item])?"on":"off"]
set $item $val
}
proc editVar {menu item} {
global $item incomingVars
append prmpt "New Value of " $item ": "
if ![catch {prompt $prmpt [set $item]} res] {
set $item $res
}
}
#================================================================================
# Instantiate a global variable to the path of a file (usually an app). As a
# side-effect, make the instantiation permanent by adding a line to 'definitions.tcl'.
proc addAppPath {name var} {
global $var
if {[catch {getfile "Find '$name' app:"} path]} {return 1}
set $var $path
addUserLine "set $var \"[quoteExpr2 $path]\""
return 0
}
proc addUserLine {line} {
global HOME
if {[file exists "$HOME:userStartup.tcl"]} {
set fid [open "$HOME:userStartup.tcl" "a"]
} else {
set fid [open "$HOME:userStartup.tcl" "w"]
}
puts $fid $line
close $fid
}
proc getFileSig {f} {
catch {lindex [ls -l $f] 5} var
return $var
}
# Look for given app sig in active processes. If not there, try to
# launch with 'path' prompting for 'path' if necessary.
# Return the real name of the app. Don't switch.
proc checkRunning {name sig path} {
global $path
foreach proc [processes] {
if {[lindex $proc 1] == $sig} {
return [lindex $proc 0]
}
}
if {![info exists $path] || ![file exists [set $path]]} {
if {[addAppPath $name $path]} return
}
if {[catch {getFileSig [set $path]}]} {
if {[addAppPath $name $path]} return
}
set sig [getFileSig [set $path]]
if {[catch {launch -f [set $path]}]} {
error "Problem with script."
}
return [file tail [set $path]]
# return [checkRunning $name $sig $path]
}
#================================================================================
# Excalibur is the only Mac spell-checker that I know of which will handle LaTeX as
# well as ordinary text.
proc spellcheckWindow {} {
global excaliburPath resumeRevert
catch {checkRunning Excalibur XCLB excaliburPath} name
if {[winInfo dirty]} {
if {[askyesno "Save '[lindex [winNames] 0]'?"] == "yes"} {
save
}
}
if {[catch {sendOpenEvent -n $name [lindex [winNames -f] 0]}] } {
beep
} else {
switchTo $name
}
set resumeRevert 1
}
proc spellcheckSelection {} {
global excaliburPath
catch {checkRunning Excalibur XCLB excaliburPath} name
if {[getPos] == [selEnd]} {
beep
message "No selection"
return;
}
copy
switchTo $name
}
#================================================================================
proc alphaHelp {} {
global HOME
edit -r -m "$HOME:Help:Alpha Commands"
}
proc tclHelp {} {
global HOME
edit -r -m "$HOME:Help:Tcl Commands"
}
set patternLibrary {
{ "Pascal to C Comments" { \{([^\}]*)\}} {/* \1 */} }
{ "C++ to C Comments" {//(.*)} {/* \1 */}}
}
proc dividingLine {} {
insertText "================================================================================\r"
}
bind 'l' <C> dividingLine
proc texDividingLine {} {
insertText "%================================================================================\r"
}
bind 'l' <C> texDividingLine TeX
proc cDividingLine {} {
insertText "//================================================================================\r"
}
bind 'l' <C> cDividingLine C
bind 'l' <C> cDividingLine C++
proc tclDividingLine {} {
insertText "#================================================================================\r"
}
bind 'l' <C> tclDividingLine Tcl
#================================================================================
if {[catch {info args oldCd}]} {
rename cd oldCd
}
proc cd args {
global HOME
if {[llength $args]} {
oldCd [string trim [eval list $args] " \{\}"]
} else {
oldCd $HOME
}
}
#================================================================================
proc getVarValue {} {
set val [listpick -p {Which var?} [lsort [info globals]]]
if {![string length $val]} return
global $val
alertnote [join [list "'$val' = " [set $val]] ""]
}
#================================================================================
proc selectParagraph {} {
set pos [getPos]
set start [paraStart $pos]
set finish [paraFinish $pos]
goto $start
select $start $finish
}
# wrapText == getText ; breakIntoLines ; replaceText
# Remove text from window, transform (join, del-ws), insert back into window.
proc fillTextByPar {from to} {
set text [getText $from $to]
regsub -all "\r(\[ \t\]*\r)+" $text "\r\r\r" text
regsub -all "(\[^\r\])\r" $text "\\1 " text
regsub -all "\[ \t\]+" $text " " text
return [breakIntoLines $text]
}
proc fillRegionByPar {{start -1} {finish -1}} {
# # if {[getPos] == [selEnd]} { return}
if {($start < 0) || ($finish < 0)} {
set start [lineStart [getPos]]
set finish [selEnd] }
if {$start >= $finish} return
goto $start
set text [fillTextByPar $start $finish]
replaceText $start $finish $text "\r"
}
#
# join Lines in region -- if no optional args, use selection
#
proc joinRegion {{from -1} {to -1}} {
if {($from < 0) || ($to < 0)} { set from [getPos] ; set to [selEnd] }
if {$from >= $to} return
set text [getText $from $to]
regsub -all "\r(\[ \t\]*\r)+" $text "\r\r\r" text
regsub -all "(\[^\r\])\r" $text "\\1 " text
replaceText $from $to $text "\r"
}
# WARNING: regsub ^$ refers to string endpts (not lines)
# FUTURE: filterLines like perl:
# replaceText[apply_to_all(cmd,split [getText [getPos] [selEnd]] "\r")]
# OR: replaceInRegion: dup_\r, $=>\r ??
#
#
# Remove text from window, transform (delete dup ws), insert back into window.
#
# inputs: message, alertnote, askyesno, listpick, prompt KILLS SELECTION.
# search: bnds = search -forward -regExpr -ignoreCase -matchWords -noabort
# -l limit pat pos
#
proc regsubInRegion {from to srch repl} {
if {![string length $srch]} return
if {$from >= $to} return
set text [getText $from $to]
regsub -all "$srch" $text "$repl" text
replaceText $from $to $text
}
# while {($pos < $to) &&
# ![catch {search -f 1 -r 1 -i 1 -m 0 "$srch" $pos} mtch]} {
# set mbeg [lindex $mtch 0]
# set pos [lindex $mtch 1]
# replaceText $mbeg $pos $repl }
#proc backSlashSub {arg} { eval [concat return "\"$arg\""] }
proc backSlashSub {arg} {
regsub -all {\\} $arg {\\\\} arg
regsub -all {\[} $arg {\\[} arg
regsub -all {\]} $arg {\\]} arg
eval [concat return "\"$arg\""]
}
proc replaceInRegion {} {
if [catch {prompt "Search RegExpr:" ""} srch] return
if [catch {prompt "Replace String:" ""} repl] return
if {![string length $srch]} return
regsubInRegion [getPos] [selEnd] \
[backSlashSub "$srch"] [backSlashSub "$repl"]
}
#
# Apply command to each line (or paragraph) in selection ;
# if no cmd arg then prompts for it
#
proc filterLines {{cmd 0} {parunit 0}} {
if {$cmd == 0} {
if {[catch { prompt "Line-filter command: " "" } cmd]} { return } }
if {![string length $cmd]} return
set unitStart lineStart
set unitEnd nextLineStart
if {$parunit} {
set unitStart paraStart
set unitEnd paraFinish }
set pos [$unitStart [getPos]]
set finish [selEnd]
if {$pos >= $finish} return
goto $pos
createTMark "filterLend" $finish
set next [$unitEnd $pos]
while {(($next > $pos) && ($pos < $finish))} {
goto [expr $next-1]
createTMark "filterLnext" $next
setMark
goto $pos
markHilite
if {[catch [list uplevel #0 "$cmd"] retval]} {
select $pos $finish
alertnote $retval
return
}
if {$next==$finish} break
set ind [lsearch -regexp [lindex [getTMarks] 0] "filterLend.*"]
set finish [lindex [lindex [lindex [getTMarks] 0] $ind] 2]
gotoTMark "filterLnext"
set pos [$unitStart [getPos]]
set next [$unitEnd $pos]
}
removeTMark "filterLend"
removeTMark "filterLnext"
}
proc filterParagraphs {{cmd 0}} { filterLines $cmd 1 }
# WARNING: deselecting sets the mark to selEnd
proc sortParagraphs {{from -1} {to -1}} {
if {($from < 0) || ($to < 0)} { set from [getPos] ; set to [selEnd] }
if {$from >= $to} return
joinRegion {$from $to}
select [getPos] [nextLineStart [getMark]]
sortLines
select [getPos] [getPos]
regsubInRegion [getPos] [getMark] "\r" "\r\r"
wrapRegion
}
#
# Sample
#
proc filterRegion {{from -1} {to -1} {cmd 0} {newwin 0}} {
if {$cmd == 0} {
if {[catch { prompt "Eval command: " "" } cmd]} { return }
}
if {![string length $cmd]} return
if {($from < 0) || ($to < 0)} { set from [getPos] ; set to [selEnd] }
if {$from >= $to} return
set pos [getPos]
set text [getText $from $to]
set text [$cmd $text]
replaceText $from $to $text "\r"
goto $pos
}
#
set lastEvaled ""
proc evaluate {} {
global lastEvaled
if {[string length $lastEvaled]} {
set p "M-x ($lastEvaled): "
} else {
set p "M-x: "
}
if {[catch {statusPrompt $p} text]} {return}
if {![string length $text]} {set text $lastEvaled}
$text
set lastEvaled $text
}
# First, define macros to bypass the electric braces.
proc ordLeftBrace {} {
insertText " \{"
}
bind {'['} <cs> ordLeftBrace
proc ordRightBrace {} {
insertText "\}"
blink [matchIt "\}" [expr [getPos]-1]]
}
bind {']'} <cs> ordRightBrace
proc quoteWord {} {
backwardWord
insertText "'"
forwardWord
insertText "'"
}
bind ''' <z> quoteWord
#================================================================================
proc tomac {fname} {
set fd [open $fname "r"]
set text [read $fd]
close $fd
set fd [open $fname "w"]
regsub "\n" $text "\r" text
puts -nonewline $fd $text
close $fd
}
proc tounix {fname} {
set fd [open $fname "r"]
set text [read $fd]
close $fd
set fd [open $fname "w"]
regsub "\r" $text "\n" text
puts -nonewline $fd $text
close $fd
}
#================================================================================
# Sets marks for file.
set mpos("") ""
proc markFile {} {
global mode
case $mode in {
"C" {return [cMarkFile]}
"TeX" {return [texMarkFile]}
"C++" {return [c++MarkFile]}
"Csh" {return [cshMarkFile]}
"Tcl" {return [tclMarkFile]}
"BRWZ" {return [browseMarkFile]}
}
}